package app

import (
	"net/http"
)

//apihello函数的功能是向链端发送一个json，获取得到的结果，返回给前端
func apihello(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	// Contract := []byte(`
	// {
	// 	"groupId" :1,
	// 	"signUserId": "test",
	// 	"contractAbi":[{"constant":false,"inputs":[{"name":"n","type":"string"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}],
	// 	"contractAddress":"0x3046c48cd02fb07d38ad9b8a2da5da0515fc55c7",
	// 	"funcName":"get",
	// 	"funcParam":[]
	// }
	// `)
	// var newcontract HelloContract
	// newcontract.GroupId = 1
	// newcontract.SignUserId = "test"
	// newcontract.ContractAddress = "0x3046c48cd02fb07d38ad9b8a2da5da0515fc55c7"
	// newcontract.FuncName = "get"
	// err := json.Unmarshal(Contract, &newcontract)
	// if err != nil {
	// 	fmt.Printf("unmarshal err=%v\n", err)
	// }
	// fmt.Println(newcontract)
	handler.Send("hello", true)
}

// //根据name获取地址
// func getaddress(name string) string {
// 	//address 应该从链端获取
// 	var address = "0x0b00b9f5aa7d4b0c1570493febc4f614c879ee74" //默认是蓝思科技的地址
// 	return address
// }

func getnamefromadd(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	add := handler.DecodePath(2)
	id := Company_Addr2ID(add)
	var res string
	if id == -1 {
		res = ""
	} else {
		res = company_name[id]
	}
	handler.Send(res, true)
}

func getaddfromname(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	name := handler.DecodePath(2)
	address := Company_Name2Addr(name)
	handler.Send(address, true)
}

func bankgetinfo(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	// body := infobody{}
	// err := handler.DecodePost(&body)
	// if err != nil {
	// 	handler.Send(err.Error(), false)
	// 	return
	// }
	//首先获取name
	name := handler.DecodePath(3)
	id := Company_Name2ID(name)
	//从链端获取余额
	var num = Company_SearchMoneyAmount(id)
	// w.Write([]byte(num))
	handler.Send(num, true)
}

func decreaseMoney(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := modifymoney{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	//首先根据用户名称获取地址
	name := body.Name
	money := body.Money
	id := Company_Name2ID(name)
	res := Company_CompanyMoneyAmountDecrease(id, money)
	print(res)
	// // address := getaddress(name)
	// //根据地址和金额修改余额
	// var money = "10000"
	// //获取修改之后的余额
	// money = "8000"
	handler.Send("success", true)
}

func increaseMoney(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := modifymoney{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	//首先根据用户名称获取地址
	name := body.Name
	money := body.Money
	id := Company_Name2ID(name)
	res := Company_CompanyMoneyAmountIncrease(id, money)
	print(res)
	handler.Send("success", true)
}

func createtransaction(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := transactionbody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	//创建交易合约
	fromName := body.From_name
	fromId := Company_Name2ID(fromName)
	toName := body.To_name
	toId := Company_Name2ID(toName)
	time := body.Time
	money := body.Money
	res := Company_CreateRecipy(fromId, toId, money, time)
	print(res)
	handler.Send("success", true)
}

func debtgetinfo(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	// body := infobody{}
	// err := handler.DecodePost(&body)
	// if err != nil {
	// 	handler.Send(err.Error(), false)
	// 	return
	// }
	//获取该债权相关信息
	name := handler.DecodePath(3)
	id := Company_Name2ID(name)
	//从链端获取债权信息
	res := Company_GetAllReceipy(id)
	handler.Send(res, true)
}

func debtTransfer(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := transferbody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	// 根据body中的信息进行债权的转移
	fromName := body.From_name
	fromId := Company_Name2ID(fromName)
	oldName := body.Ori_name
	oldId := Company_Name2ID(oldName)
	toName := body.To_name
	toId := Company_Name2ID(toName)
	time := body.Time
	money := body.Money
	res := Company_TransferReceipy(fromId, oldId, toId, money, time)
	print(res)
	handler.Send("success", true)
}

func debtbankhelp(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := bankhelpbody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	fromName := body.From_name
	fromId := Company_Name2ID(fromName)
	toName := body.To_name
	toId := Company_Name2ID(toName)
	time := body.Time
	money := body.Money
	res := Company_BankHelpCompany(fromId, toId, money, time)
	print(res)
	handler.Send("success", true)
}

func debtpay(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := paydebtbody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	fromName := body.From_name
	fromId := Company_Name2ID(fromName)
	toName := body.To_name
	toId := Company_Name2ID(toName)
	money := body.Money
	res := Company_PayReceipy(fromId, toId, money)
	print(res)
	handler.Send("success", true)
}

func paybank(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := paybankbody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	name := body.Name
	id := Company_Name2ID(name)
	money := body.Money
	res := Company_PayBank(id, money)
	print(res)
	handler.Send("success", true)
}
